package GUI;

import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.Rectangle;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Client_logic.Type;
import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;

/**
 * This class use for add product to catalog type. 
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 *
 *
 */
public class AddProductToTypeForm extends TypeManagementForm 
{
	
	private static final long serialVersionUID = 1L;
	private JLabel productIDLabel = null;
	private JComboBox productIDComboBox = null;
	private boolean addflag;
	private boolean removeflag;
	private Type type = null; 
	
	public AddProductToTypeForm()
	{
		super();
		initialize(); 
	}
	
	/**
	 * This method initializes AddProductToTypeForm
	 */
	private void initialize() 
	{
		type = new Type(0,"");
		addflag = false;
		removeflag = false;
		this.setSize(600, 250);
		updateForm();
		getSaveButton().setText("Add");	
		getIdLabel().setText("Type ID");
		getNameLabel().setText("Product ID");
		getTypesIDLabel().setText("All type");
		getProductsIDLabel().setText("All product:");
		getAddbuttonLabel().setText("Selected info:");
		getIdTextField().setText("");
		getNameTextField().setText("");
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		getUpdateButton().setEnabled(false);
		productIDLabel = new JLabel();
		productIDLabel.setBounds(new Rectangle(425, 20, 130, 22));
		productIDLabel.setText("All products in type:");
		addSaveButton();
		addremoveButton();
		addTypesIDComboBox();
		getProductsIDTextAreaScrollPane().setBounds(425, 40, 160, 160);
		this.add(productIDLabel, null);
		this.add(getProductIDComboBox(), null);
		
	}
	
	/**
	 * This method update form comboBoxes to data from data base
	 */
	public void updateForm()
	{
		updateTypesIDComboBox();
		updateSectionsIDComboBox();
	}
	
	/**
	 * This method initializes typesIDComboBox
	 */
	private void addTypesIDComboBox()
	{
		getTypesIDComboBox().addActionListener(new java.awt.event.ActionListener() 
		{
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{			
				String str = (String)getTypesIDComboBox().getSelectedItem();
				if (str.equals(":"))
					return;
				String typeID = str.substring(0, str.indexOf(":"));
				getIdTextField().setText(typeID);				
				String[] productslist = null;
				try
				{
					ConnectionToServer connectiontoserver = new ConnectionToServer();		
					productslist = (String[]) connectiontoserver.loadTypeProduct
					(Integer.parseInt(typeID));
					if (productslist == null)
					{
						getProductsIDTextArea().setText("");
					}							
					else
					{
						String tmpstr = "";
						int len = productslist.length;
						for(int i=0; i < len; i++)
						{
							tmpstr = tmpstr+(productslist[i]+"\n");
						}
						getProductsIDTextArea().setText(tmpstr);
					}
				}catch(ClientNotInitializesException exc)
				{
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
				catch(Exception except)
				{
					JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
			}
		});		
	}
	
	/**
	 * This method initializes typesIDComboBox	
	 */
	public JComboBox getProductIDComboBox()
	{
		if (productIDComboBox == null) 
		{
			productIDComboBox = new JComboBox();
			productIDComboBox.setBounds(new Rectangle(290, 40, 127, 25));
			long[] productid = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				productid = (long[]) connectiontoserver.loadAllProductsID();
				if (productid == null)
				{
					JOptionPane.showMessageDialog(null, "Catalogue is EMPTY!!!");
					productIDComboBox.addItem(0);
				}	
				else
				{
					int len = productid.length;
					for(int i=0; i < len; i++)
					{
						productIDComboBox.addItem(productid[i]);
					}	
				}
			}catch(Exception e)
			{
				productIDComboBox.addItem(0);
			}			
			
			productIDComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					getNameTextField().setText(String.valueOf((Long)productIDComboBox.getSelectedItem()));
				}
			});		
			
		}
		return productIDComboBox;
	}
	
	/**
	 * This method initializes SaveButton ActionListener
	 */
	private void addSaveButton()
	{
		getSaveButton().addActionListener(new java.awt.event.ActionListener() 
		{
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{			
				if (addflag)
				{
					saveSection();										
				}
				else
				{
					getSaveButton().setText("OK");
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					getAddbuttonLabel().setText("Add this product to the selected type:");
					getRemoveButton().setEnabled(false);
					addflag = true;
				}
			}
		});		
	}
	
	/*
	 * This method add selected product to the selected type
	 */
	private void saveSection()
	{
		int typeID = 0;
		long productID = 0;
		try
		{
			typeID = Integer.parseInt(getIdTextField().getText());
			productID = Long.parseLong(getNameTextField().getText());
		}
		catch(Exception exc){}
		
		if (typeID > 0 && productID > 0)
		{
			type.setTypeID(typeID);
			try 
			{
				if (type.addProductToType(productID))
				{
					JOptionPane.showMessageDialog(null, "Product: "+productID+" successfuly added to type: "+typeID);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Product not added to type","",JOptionPane.WARNING_MESSAGE);
			} catch (ClientNotInitializesException e1) 
			{
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else
		{
			JOptionPane.showMessageDialog(null, "PLZ select type and product","",JOptionPane.WARNING_MESSAGE);
			restore();
		}
	}
	
	/**
	 * This method initializes removeButton ActionListener
	 */
	private void addremoveButton()
	{
		getRemoveButton().addActionListener(new java.awt.event.ActionListener() 
		{
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{	
				if (removeflag)
				{
					removeProductFormType();										
				}
				else
				{
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					getAddbuttonLabel().setText("Remove this product from the type:");
					getSaveButton().setEnabled(false);
					removeflag = true;
				}				
	}
	
	/*
	 * This method remove selected product from the selected type
	 */
	private void removeProductFormType()
	{
		int typeID = 0;
		long productID = 0;
		try
		{
			typeID = Integer.parseInt(getIdTextField().getText());
			productID = Long.parseLong(getNameTextField().getText());
		}
		catch(Exception exc){}
		
		if (typeID > 0 && productID > 0)
		{
			type.setTypeID(typeID);
			try 
			{
				if (type.removeProductFromType(productID))
				{
					JOptionPane.showMessageDialog(null, "Product: "+productID+" successfuly remove from type: "+typeID);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Product not remove!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (HeadlessException e1) 
			{
				JOptionPane.showMessageDialog(null, "System ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			
			} catch (ClientNotInitializesException e1) 
			{
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}
		}
		else
		{
			JOptionPane.showMessageDialog(null, "PLZ select type and product","",JOptionPane.WARNING_MESSAGE);
			restore();
		}
	}
});		
	}
	
	/**
	 * This method restore the form button  
	 */
	public void restore()
	{
		getSaveButton().setText("Add");	
		getAddbuttonLabel().setText("Selected info:");	
		addflag = false;
		removeflag = false;
		getIdTextField().setForeground(Color.black);
		getNameTextField().setForeground(Color.black);
		getAddbuttonLabel().setForeground(Color.black);
		getIdTextField().setText("");
		getNameTextField().setText("");
		getRemoveButton().setEnabled(true);
		getSaveButton().setEnabled(true);		
	}
}
